package com.fs.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class BeanFactories {


    public static Properties properties = new Properties();

    private static Map<String,Object> beanContainer = new HashMap<String,Object>();

    static {
        InputStream is = BeanFactories.class.getClassLoader().getResourceAsStream("Bean.properties");
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static <T> T getBean(Class<T> clazz) {

        String className = properties.getProperty(clazz.getSimpleName());

        if(beanContainer.containsKey(className))
            return (T)beanContainer.get(className);



        Class<?> aClass = null;

        try {
            aClass = Class.forName(className);

            T t = (T) aClass.newInstance();

            beanContainer.put(className,t);

            return t;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }


        return null;

}
}
